package com.agileplex.products.acc.commands;

import java.io.File;
import java.io.IOException;

import com.agileplex.products.acc.CommandExecutionException;
import com.agileplex.products.acc.Configuration;
import com.agileplex.products.acc.ConfigurationException;
import com.agileplex.products.acc.Session;
import com.agileplex.products.acc.results.CommandResult;

/**
 * Open session command
 *
 * @creator Rui Tang
 * @date 2008-06-18
 *
 * @LastChanged
 * @LastChangedBy $LastChangedBy: tangrui@agileplex.com $
 * @LastChangedDate $LastChangedDate: 2008-06-22 16:08:49 +0000 (Sun, 22 Jun 2008) $
 * @LastChangedRevision $LastChangedRevision: 7 $
 */
public class OpenSessionCommand extends Command {

    public String configFileName;

    public OpenSessionCommand(String name) {
        super(name);
    }

    public void setConfigurationFileName(String configFileName) {
        this.configFileName = configFileName;
    }

    @Override
    public CommandResult exec(LexicalParser lexicalParser, Session session) throws CommandExecutionException {
        File configFile = new File(this.configFileName);
        if (!configFile.exists()) {
            throw new CommandExecutionException("Configuration file " + this.configFileName + " is not found.");
        }

        try {
            Configuration c = new Configuration(configFile);
            Session s = c.createSession();
            s.open(session.getInputStream(), session.getOutputStream());
            return new CommandResult();
        } catch (ConfigurationException e) {
            throw new CommandExecutionException(e);
        } catch (IOException e) {
            throw new CommandExecutionException(e);
        }
    }

}
